package top.oauth2.api.demo.controller;

import com.alibaba.excel.util.FileUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.xphu.zip.service.DownloadFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.util.HashMap;

/**
 * @author xphu
 * @version 1.0
 * @date 2021/9/2 14:50
 */
@RestController
public class EasyExcelDemoController {

    @GetMapping("download")
    public void downloadExcel(HttpServletResponse response, HttpServletRequest request) throws Exception {
        /**
         * 三种图片展示方式
         *  - file
         *  - byte[]
         *  - InputStream
         */
        File file = new File("C:\\Users\\24267\\Pictures\\test\\1.jpg");
        File file2 = new File("C:\\Users\\24267\\Pictures\\test\\2.jpg");
        byte[] bytes = FileUtils.readFileToByteArray(new File("C:\\Users\\24267\\Pictures\\test\\3.jpg"));
        InputStream inputStream = FileUtils.openInputStream(new File("C:\\Users\\24267\\Pictures\\test\\bg.jpg"));

        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "湘潭瑞欣电工材料有限公司");
        map.put("codVal", 12.5);
        map.put("pphVal", 15.5);
        map.put("codStandardVal", 34);
        map.put("pphStandardVal", 15.5);
        map.put("isHunPai", "是");
        map.put("img1", file);
        map.put("img2", file2);
        map.put("img3", new URL("https://file.5m4.top/Snipaste_2021-08-31_11-00-08.png"));
        map.put("img4", inputStream);
        map.put("img5",  bytes);
        String templateName = "excel/template.xlsx";
        DownloadFile.build().downloadExcel(templateName, "测试文件", map, response);
    }
}
